﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
		<title>ASP.NET Boilerplate Module System</title>
		<link type="text/css" rel="stylesheet" href="bootstrap.min.css" />
	</head>

	<body>

		<div class="document-contents">

			<h3 id="DocIntroModules">Introduction to modules</h3>
			<p>ASP.NET Boilerplate provides an infrastructure to build modules and compose 
them to create an application. A module can depend on another module. Generally, an assembly is considered 
as a module. A 
module is defined with a class that is derived from <strong>AbpModule</strong>. 
Say that we're developing a Blog module that can be used in different 
applications. Simplest module definition can be as shown below:</p>
			<pre lang="cs">public class MyBlogApplicationModule : AbpModule
{
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
    }
}</pre>
			<p>ASP.NET Boilerplate scans all assemblies and finds all classes those are 
devired from AbpModule class. If you created an application with more than one 
assembly, it's suggested to create a module definition for each assembly.</p>
			<h3 id="DocModuleLifecycleEvents">Lifecycle events</h3>
			<p>ASP.NET Boilerplate calls some specific methods of modules on application 
startup and shutdown. You can override these methods to perform some specific 
tasks.</p>
			<p>ASP.NET Boilerplate calls these methods <strong>ordered by dependecies</strong>. If module A depends on module B, module B is initialized before module A. Exact order of 
startup methods: PreInitialize-B, PreInitialize-A, Initialize-B, Initialize-A, PostInitialize-B and PostInitialize-A. This is true for all dependency graph. 
Shutdown method is also similar but in reverse order.</p>
			<h4 id="DocModulePreInit">PreInitialize</h4>
			<p>This method is called first when application starts. You can write some 
specific code here to run before dependency injection registrations. For 
example, if you create a conventional registration class, you should register it 
here (using IocManager.AddConventionalRegisterer method). You can 
register to events of IOC container.. so on.</p>
			<h4 id="DocModuleInit">Initialize</h4>
			<p>It's the usual place where
				<a href="/Pages/Documents/Dependency-Injection">dependency injection</a> 
registration should be done. It's generally done using IocManager.RegisterAssemblyByConvention method. 
If you want to define custom dependency registration, see dependency injection 
documentation.</p>
			<h4 id="DocModulePostInit">PostInitialize</h4>

			<p>This method is called lastly in startup progress. It's safe to resolve a 
dependency here.</p>
			<h4 id="DocModuleShutdown">Shutdown</h4>
			<p>This method is called when the application shuts down.</p>
			<h3 id="DocModuleDepend">Module dependencies</h3>

			<p>A module can be dependent to another. ASP.NET Boilerplate tries to resolve 
these dependencies automatically. But it's suggested to explicitly declare dependencies 
using DependsOn attribute like below:</p>
			<pre lang="cs">[DependsOn(typeof(MyBlogCoreModule))]
public class MyBlogApplicationModule : AbpModule
{
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
    }
}</pre>
			<p>Thus, we declare to ASP.NET Boilerplate that MyBlogApplicationModule is 
depended to MyBlogCoreModule and the core module should be initialized before 
the application module.</p>
			<h3 id="DocCustomModule">Custom module methods</h3>
			<p>Your modules also can have custom methods those can be used by other modules 
depend on this module. Assume that MyModule2 depends on MyModule1 and wants to 
call a method of MyModule1 in PreInitialize event.</p>

			<pre lang="cs">public class MyModule1 : AbpModule
{
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
    }

    public void MyModuleMethod1()
    {
        //this is a custom method of this module
    }
}

[DependsOn(typeof(MyModule1))]
public class MyModule2 : AbpModule
{
    private readonly MyModule1 _myModule1;

    public MyModule2(MyModule1 myModule1)
    {
        _myModule1 = myModule1;
    }

    public override void PreInitialize()
    {
        _myModule1.MyModuleMethod1(); //Call MyModule1's method
    }

    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
    }
}</pre>

			<p>Here, we constructor-injected MyModule1 to MyModule2, so MyModule2 can call 
MyModule1's custom method.</p>
		</div>
	</body>

</html>
